<?php

namespace IDock\Insurance\QHIC;

use IDock\Common\BaseInterface\BuilderInterface;
use IDock\Http\Request;
use IDock\Http\Response;
use IDock\Insurance\QHIC\Request\AllQHICRequest;
use IDock\Insurance\QHIC\Request\PremiumInquiryRequest;
use IDock\Insurance\QHIC\Response\AllQHICResponse;

class Builder implements BuilderInterface
{

    public function getConfig(): array
    {
        return $this->config;
    }

    protected $config;

    protected $partnerAccountCode;

    protected $recordCode;

    protected $CDataSource;

    protected $CBrkrCde;

    public $secret;

    public $notifyUrl;

    public function execute(Request $request): Response
    {
        /*** @var AllQHICRequest $request */
        $request->loadConfig($this->config);

        $request->setRequestId(md5(uniqid()));
        $request->setHeaders(['Content-Type' => 'application/json']);
        if(property_exists($request,'CBrkrCde')){
            $request->CBrkrCde = $this->CBrkrCde;
        }
        $requestData = [
            'Head' => [
                'CDataSource' => $this->CDataSource,
                'RequestType' => $request->requestType,
                'TransDate' => date('YmdHis'),
                'TransId' => $request->getRequestId(),
            ],
            'Body' =>  $request->generate()
        ];
        $request->setPreParameter($requestData);
        $request = $this->sign($request);

        return $this->request($request);
    }

    public function generate(Request $request): array
    {
        return $request->generate();
    }

    public function sign(Request $request): Request
    {
        $data = $request->getPreParameter();
        $data['Body'] = $this->encryptCBC(json_encode($data['Body']),$this->secret);
        $request->setBizParam(json_encode($data));
        return $request;
    }

    private function encryptCBC($data, $key) {
        $options = OPENSSL_RAW_DATA;
        $cipher = 'AES-128-CBC';
        $iv = '0123456789ABCDEF';
        $key  = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
        $encrypted = openssl_encrypt($data, $cipher, $key, $options, $iv);
        return base64_encode($encrypted);
    }

    public function exceptionResponse($request, $errorMessage): Response
    {
        $response = $request->getResponse();

        $response = new $response;
        $response->setCode('500');
        $response->setError($errorMessage);

        return $response;
    }

    public function request(Request $request): Response
    {
        /*** @var AllQHICResponse $response */
        $response = $request->request($request);
        $response->setVisitor($request->getVisitor());

        if ($response->isSuccess()) {
            $response->decrypt($this->secret,$request);
            $response->printerInit();
        } else {
            $request->responseLogs($request->getRequestId(),$response->getBody());
        }
        return $response;
    }

    public function encrypt(Request $request)
    {

    }

    public function setToken($token)
    {
        $this->token = $token;
    }
}


